﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    //Add By Ben 20130429
    //取得多國語言
    [System.Web.Services.WebMethod]
    public static Dictionary<string, string> GetLanguage(IList ColLan)
    {

        Dictionary<string, string> m_ColLans = new Dictionary<string, string>();
        foreach (string m_Clo in ColLan)
        {
            try
            {
                m_ColLans.Add(m_Clo, HttpContext.GetGlobalResourceObject("MessageResource", m_Clo).ToString());
            }
            catch (Exception ex)
            {
                var m_ex = ex.Message;
            }
        }
        return m_ColLans;
    }


    int level = 0;
    string deptID = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        level = BLogical.GetDeptLevelbyUser(User.Identity.Name);
        deptID = BLogical.GetDeptIDByUserID(User.Identity.Name);

        if (!IsPostBack)
        {
            SDS_KRJ_BULLETIN_MASTER.SelectCommand =
@"
SELECT * FROM 
(SELECT  BULLETIN_ID, BULLETIN_TEXT_TW, BULLETIN_SPEC_TW, BULLETIN_TEXT_EN, BULLETIN_SPEC_EN, BULLETIN_TEXT_GB, BULLETIN_SPEC_GB, START_DATE, END_DATE, STATUS, REMARK, CRT_ID, CRT_DATE, CRT_TIME, UPD_ID, UPD_DATE, UPD_TIME FROM TG.KRJ_BULLETIN_MASTER 
WHERE STATUS='Y' AND START_DATE IS NOT NULL 
AND START_DATE <= TO_CHAR(SYSDATE,'YYYYMMDD') AND END_DATE >=  TO_CHAR(SYSDATE,'YYYYMMDD')
ORDER BY START_DATE DESC)
WHERE ROWNUM <= 30
";
        }
    }

    KRJEntities krj = new KRJEntities();
    AS400Entities as400 = new AS400Entities();

    protected void GV_BULLETIN_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string bid = ((Label)e.Row.FindControl("lbl_BULLETIN_ID")).Text;

            //處理日期格式
            Label lbl_Date = e.Row.FindControl("lbl_Date") as Label;
            Label lbl_START_DATE = e.Row.FindControl("lbl_START_DATE") as Label;
            if (!string.IsNullOrEmpty(lbl_START_DATE.Text))
            {
                lbl_Date.Text = CommonUtil.TransferyyyyMMddToDate(lbl_START_DATE.Text);
            }

            Label lbl_END_DATE = e.Row.FindControl("lbl_END_DATE") as Label;
            if (!string.IsNullOrEmpty(lbl_END_DATE.Text))
            {
                lbl_Date.Text += " - " + CommonUtil.TransferyyyyMMddToDate(lbl_END_DATE.Text);
            }

            //處理公告內容
            HyperLink HyperLink_TEXT = (HyperLink)e.Row.FindControl("HyperLink_TEXT");
            string language = 
                Request.Cookies["KerryLang"] != null ? Request.Cookies["KerryLang"].Values["Language"].ToString() : "zh-TW";
            switch (language)
            {
                case "zh-TW":
                    HyperLink_TEXT.Text = ((Label)e.Row.FindControl("lbl_BULLETIN_TEXT_TW")).Text;
                    break;
                case "zh-CN":
                    HyperLink_TEXT.Text = ((Label)e.Row.FindControl("lbl_BULLETIN_TEXT_GB")).Text;
                    break;
                case "en-US":
                    HyperLink_TEXT.Text = ((Label)e.Row.FindControl("lbl_BULLETIN_TEXT_EN")).Text;
                    break;
            }
            HyperLink_TEXT.NavigateUrl = string.Format("~/Announcement/AnnouncementPage.aspx?bid={0}", bid);

            //處理附件                       
            PlaceHolder PlaceHolder_Files = e.Row.FindControl("PlaceHolder_Files") as PlaceHolder;
            FileUtil.DisplayAttachmentFiles(PlaceHolder_Files, bid);

            //處理公告檢視權限-依據KRJ_BULLETIN_CONDITION資料表中的設定對應五個階層
            bool IsShowBULLETIN = false;
            string[] condition = new string[] { "ALL", deptID };
            var bulletins = krj.KRJ_BULLETIN_CONDITION.Where(b => b.BULLETIN_ID == bid);
            var BIF41 = as400.AS400_BIF41.Where(a => a.BI4101 == deptID).SingleOrDefault();
            //公司級：BI4119 - NAME1
            //營級：BI4115 - NAME2
            //處級：BI4114 - NAME3
            //區級：BI4113 - NAME4            

            switch (level)
            {
                case 1://公司級
                    foreach (var bulletin in bulletins)
                    {
                        if (bulletin.NAME1 == "ALL" || bulletin.NAME1 == deptID)
                        {
                            IsShowBULLETIN = true;
                        }
                    }
                    break;
                case 2://營級
                    foreach (var bulletin in bulletins)
                    {
                        if (bulletin.NAME1 == "ALL")
                        {
                            IsShowBULLETIN = true;
                        }
                        else
                        {
                            if (bulletin.NAME2 == deptID)
                            {
                                IsShowBULLETIN = true;
                            }
                            else if (bulletin.NAME2 == "ALL")  //如果是ALL還需要判斷上一層是否為此部門的對應上層部門
                            {
                                if (BIF41.BI4119 == bulletin.NAME1)
                                {
                                    IsShowBULLETIN = true;
                                }
                            }
                        }

                    }
                    break;
                case 3:
                    foreach (var bulletin in bulletins)
                    {
                        if (bulletin.NAME1 == "ALL")
                        {
                            IsShowBULLETIN = true;
                        }
                        else
                        {
                            if (bulletin.NAME3 == deptID)
                            {
                                IsShowBULLETIN = true;
                            }
                            else if (bulletin.NAME3 == "ALL") //往上推兩層
                            {
                                if (BIF41.BI4115 == bulletin.NAME2)
                                {
                                    IsShowBULLETIN = true;
                                }
                                else if (bulletin.NAME2 == "ALL")
                                {
                                    if (BIF41.BI4119 == bulletin.NAME1)
                                    {
                                        IsShowBULLETIN = true;
                                    }
                                }
                            }
                        }
                    }
                    break;
                case 4:
                    foreach (var bulletin in bulletins)
                    {
                        if (bulletin.NAME1 == "ALL")
                        {
                            IsShowBULLETIN = true;
                        }
                        else
                        {
                            if (bulletin.NAME4 == deptID)
                            {
                                IsShowBULLETIN = true;
                            }
                            else if (bulletin.NAME4 == "ALL") //往上推三層
                            {
                                if (BIF41.BI4114 == bulletin.NAME3)
                                {
                                    IsShowBULLETIN = true;
                                }
                                else if (bulletin.NAME3 == "ALL") //往上推兩層
                                {
                                    if (BIF41.BI4115 == bulletin.NAME2)
                                    {
                                        IsShowBULLETIN = true;
                                    }
                                    else if (bulletin.NAME2 == "ALL")
                                    {
                                        if (BIF41.BI4119 == bulletin.NAME1)
                                        {
                                            IsShowBULLETIN = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case 5:
                    foreach (var bulletin in bulletins)
                    {
                        if (bulletin.NAME1 == "ALL")
                        {
                            IsShowBULLETIN = true;
                        }
                        else
                        {
                            if (bulletin.NAME5 == deptID)
                            {
                                IsShowBULLETIN = true;
                            }
                            else if (bulletin.NAME5 == "ALL")//往上推四層
                            {
                                if (BIF41.BI4113 == bulletin.NAME4)
                                {
                                    IsShowBULLETIN = true;
                                }
                                else if (bulletin.NAME4 == "ALL")//往上推三層
                                {
                                    if (BIF41.BI4114 == bulletin.NAME3)
                                    {
                                        IsShowBULLETIN = true;
                                    }
                                    else if (bulletin.NAME3 == "ALL") //往上推兩層
                                    {
                                        if (BIF41.BI4115 == bulletin.NAME2)
                                        {
                                            IsShowBULLETIN = true;
                                        }
                                        else if (bulletin.NAME2 == "ALL")
                                        {
                                            if (BIF41.BI4119 == bulletin.NAME1)
                                            {
                                                IsShowBULLETIN = true;
                                            }
                                        }
                                    }

                                }
                            }
                        }
                    }
                    break;
            }

            e.Row.Visible = IsShowBULLETIN;
        }
    }



    protected void btn_More_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Announcement/Announcement.aspx");
    }
}